<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_ippi extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('admin_model', 'm_admin', TRUE);
    }
    
    function index() {
        $data = array('title' =>  'ADMIN Web I P P I');
        $this->load->view('mimin/login/index',$data);
    }
    
    function login() {
        $id     = $this->input->post("username");
        $pass   = md5($this->input->post("password"));
        
        if($this->m_admin->login_check($id, $pass))
        {
            $admin_user = $this->m_admin->select_id($id);                
            $this->session->set_userdata('admin_user',$admin_user);
            redirect('admin_home', 'location');
        }
        else
        {
            $data = array('title' =>  'ADMIN Web I P P I','msg'=>'Username atau Password salah');
            $this->load->view('mimin/login/index',$data);
        }
    }
    function edit(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            $this->form_validation->set_rules('judul','Judul','required');
            $this->form_validation->set_rules('content','Isi Konten','required');
            if($this->form_validation->run() == FALSE){
                // set common properties
                $data = array(
                    'web_title'     =>  'Edit Data Profile',
                    'main_view'     =>  "mimin/admin/edit",
                    'list_data'     =>  $this->m_admin->get_by_id($this->session->userdata('admin_user')->id_admin)
                );
                $this->load->view("mimin/template/template_editor",$data);
            }
            else{
                
            }
        }
    }
    function cpass_act(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                "status"    =>  0,
                "msg"       =>  "Silahkan Login Kembali"
            );
            echo json_encode($output);exit();
        }
        else{
            $this->form_validation->set_rules('opass','Judul','required');
            $this->form_validation->set_rules('npass','Isi Konten','required');
            $this->form_validation->set_rules('cpass','Isi Konten','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    "status"    =>  0,
                    "msg"       =>  "Field Old Password, New Password and Confirmation New Password must be filled"
                );
                echo json_encode($output);exit();
            }
            else{
                $count = $this->m_admin->check_pass($this->session->userdata('admin_user')->id_admin,md5($this->input->post('opass')));
                if($count > 0){
                    $new_pass = array(
                        'password'  => md5($this->input->post('npass')) 
                    );
                    $status = $this->m_admin->update($this->session->userdata('admin_user')->username,$new_pass);
                    if($status){
                        $output = array(
                            "status"    =>  1,
                            "msg"       =>  "Password baru telah diset"
                        );
                        echo json_encode($output);exit();
                    }
                    else{
                        $output = array(
                            "status"    =>  0,
                            "msg"       =>  "Gagal Mengubah password, ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $output = array(
                        "status"    =>  0,
                        "msg"       =>  "Password lama salah"
                    );
                    echo json_encode($output);exit();
                }
            }
        }
    }
    function logout()
    {
            $this->session->sess_destroy();
//            $this->session->unset_userdata('admin_user');
            redirect('admin_ippi','refresh');
    }
}

/* End of file admin_ippi.php */
/* Location: ./application/controllers/admin_ippi.php */